﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;

namespace MO
{
    public class Log
    {
        /// <summary>
        /// 日志目录。
        /// </summary>
        public static string LogPath;
        static List<string> runList = new List<string>();
        static Log()
        {
            Init();
        }
        static void Init()
        {
            LogPath = ConfigAccess.RunPath + "Log\\";
            if (!Directory.Exists(LogPath))
                Directory.CreateDirectory(LogPath);
            ThreadOperating t = new ThreadOperating(delegate()
            {
                while (true)
                {
                    if (runList.Count > 0)
                    {
                        string[] strs = null;
                        lock (runList)
                        {
                            strs = runList.ToArray();
                            runList.Clear();
                        }
                        string filePath = LogPath + "\\Run" + DateTime.Now.ToString("yyyyMMddHH") + ".log";
                        FileOperation.AppendAllText(filePath, string.Join("", strs));
                    }
                    Thread.Sleep(100);
                }
            });
            t.Start();
        }
        public static void WriteError(string str)
        {
            FileOperation.AppendAllText(LogPath + "\\Error" + DateTime.Now.ToString("yyyyMMddHH") + ".log", DateTime.Now.ToString() + "  " + str + "\r\n\r\n");
        }
        public static void WriteRun(string str)
        {
            lock (runList)
            {
                runList.Add(DateTime.Now.ToString() + "  " + str + "\r\n");
            }
        }
    }
}
